/**
 * Copyright (c) 2010 sonntagsfarben.de
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package de.sonntagsfarben.display
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;

	public class CoreBitmap extends Bitmap
	{
		private var _scaleMode : String = ScaleMode.EXACT_FIT;
		private var _alignMode : String = Align.NO_ALIGN;
		private var _width : Number = -1;
		private var _height : Number = -1;

		public function CoreBitmap( bitmapData : BitmapData = null , pixelSnapping : String = "auto" , smoothing : Boolean = true )
		{
			super(bitmapData, pixelSnapping, smoothing);
			if(bitmapData){
				_width = bitmapData.width;
				_height = bitmapData.height;
			}
		}


		/****************************************
		 *			PRIVATE
		 ****************************************/
		private function _resize() : void
		{
			if ( bitmapData == null )
				return;

			var bitmapRatio : Number = bitmapData.width / bitmapData.height;
			var currentRatio : Number = _width / _height;

			switch ( _scaleMode ) {

				case ScaleMode.FULL_FIT:

					if ( bitmapRatio == currentRatio )
					{
						super.width = _width;
						super.height = _height;
					}
					else if ( bitmapRatio < currentRatio )
					{
						super.width = _width;
						super.height = _width / bitmapRatio;
					}
					else
					{
						super.width = _height * bitmapRatio;
						super.height = _height;
					}

					break;



				case ScaleMode.MAINTAIN_ASPECT_RATIO:

					if ( bitmapRatio > currentRatio )
					{
						super.width = _width;
						super.height = _width / bitmapRatio;
					}
					else
					{
						super.width = _height * bitmapRatio;
						super.height = _height;
					}

					break;

				
				case ScaleMode.NO_SCALE:
				
					super.width = bitmapData.width;
					super.height = bitmapData.height;
				
					break;
				
				case ScaleMode.EXACT_FIT:
				default:
					super.width = _width;
					super.height = _height;
			}
			
			
			_align();
		}

		
		private function _align() : void
		{
			switch(_alignMode) {
				case Align.CENTER:
					super.x = int((width - actualWidth) * 0.5);
					super.y = int((height - actualHeight) * 0.5);
					break;
				case Align.TOP_CENTER:
					super.x = int((width - actualWidth) * 0.5);
					break;
				case Align.TOP_RIGHT:
					super.x = int(width - actualWidth);
					break;
				case Align.RIGHT:
					super.x = int(width - actualWidth);
					super.y = int((height - actualHeight) * 0.5);
					break;
				case Align.LEFT:
					super.y = int((height - actualHeight) * 0.5);
					break;
				case Align.BOTTOM:
				case Align.BOTTOM_LEFT:
					super.y = int((height - actualHeight));
					break;
				case Align.BOTTOM_RIGHT:
					super.y = int((height - actualHeight));
					super.x = int(width - actualWidth);
					break;
				case Align.BOTTOM_CENTER:
					super.x = int((width - actualWidth) * 0.5);
					super.y = int((height - actualHeight));
					break;
				default:
					break;
			}
		}

		
		/****************************************
		 *			PUBLIC
		 ****************************************/
		public function setSize(width : Number, height : Number) : void
		{
			_width = width;
			_height = height;
			_resize();
		}

		
		/****************************************
		 *			GETTER-SETTER
		 ****************************************/
		override public function set width( value : Number ) : void
		{
			_width = value;
			_resize();
		}

		
		override public function get width() : Number 
		{
			return _width;
		}

		
		override public function set height( value : Number ) : void
		{
			_height = value;
			_resize();
		}

		
		override public function get height() : Number 
		{
			return _height;
		}
		
		
		
		/*public function get x():Number
		{
			return _x;
		}
		
		public function set x(value:Number):void
		{
			_x = value;
		}
		
		public function get y():Number
		{
			return _y;
		}
		
		public function set y(value:Number):void
		{
			_y = value;
		}*/
		
		public function get actualWidth() : Number
		{
			return super.width;
		}

		
		public function get actualHeight() : Number
		{
			return super.height;
		}

		
		override public function set bitmapData( value : BitmapData ) : void
		{
			super.bitmapData = value;

			_width = bitmapData.width;
			_height = bitmapData.height;
		}

		
		public function get scaleMode() : String
		{
			return _scaleMode;
		}

		
		public function set scaleMode( scaleMode : String ) : void
		{
			_scaleMode = scaleMode;
			_alignMode = (_scaleMode == ScaleMode.EXACT_FIT) ? Align.NO_ALIGN : _alignMode;
			_resize();
		}

		
		public function get align() : String
		{
			return _alignMode;
		}

		
		public function set align(align : String) : void
		{
			_alignMode = align;
			_align();
		}
	}
}